package xin.marcher.module.inventory.controller;

import cn.hutool.core.collection.CollUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import xin.marcher.module.common.core.JsonResult;
import xin.marcher.module.common.tair.TairCache;
import xin.marcher.module.common.utils.SnowflakeIdWorker;
import xin.marcher.module.inventory.cache.InventoryBucketCache;
import xin.marcher.module.inventory.domain.request.InventorBucketRequest;
import xin.marcher.module.inventory.domain.request.InventoryRequest;
import xin.marcher.module.inventory.domain.response.ProductInventoryDTO;
import xin.marcher.module.inventory.service.InventoryBucketService;
import xin.marcher.module.inventory.service.InventoryService;

import javax.annotation.Resource;
import java.util.Set;

/**
 * 库存
 */
@Slf4j
@RestController
@RequestMapping("/product/inventory")
public class InventoryController {

    @Resource
    private InventoryBucketService inventoryBucketService;

    @Resource
    private InventoryService inventoryService;

    @Resource
    private InventoryBucketCache cache;

    @Resource
    private TairCache tairCache;

    @RequestMapping("/init")
    public void inventoryInit(@RequestBody InventorBucketRequest request) {
        // 清除本地缓存数据
        cache.getCache().invalidateAll();
        // 清除tair中的数据
        Set<String> keys = tairCache.getJedis().keys("*" + request.getSellerId() + request.getSkuId() + "*");
        if (CollUtil.isNotEmpty(keys)) {
            tairCache.mdelete(Lists.newArrayList(keys));
        }

        // TODO 这里模拟指定单号，实际接口需要外部传入
        request.setInventorCode(SnowflakeIdWorker.getCode());

        // 初始化库存信息
        inventoryBucketService.inventorBucket(request);
    }

    @RequestMapping("/deduct")
    public JsonResult deductProductStock(@RequestBody InventoryRequest request) {
//        long start = System.currentTimeMillis();
        // 这里模拟指定单号，实际接口需要外部传入
        request.setOrderId(SnowflakeIdWorker.getCode());
        JsonResult result = inventoryService.deductProductStock(request);
//        log.error("本次扣减库存耗时{}",System.currentTimeMillis()-start);
        return result;
    }

    @RequestMapping("/inventorBucket")
    public void inventorBucket(@RequestBody InventorBucketRequest request) {
        // 这里模拟指定单号，实际接口需要外部传入
        request.setInventorCode(SnowflakeIdWorker.getCode());

        // 增加库存
        inventoryBucketService.inventorBucket(request);
    }

    @RequestMapping("/queryProductInventoryList")
    public JsonResult inventoryInit(@RequestBody InventoryRequest request) {
        ProductInventoryDTO productInventoryDTO = inventoryBucketService.queryProductInventoryList(request);
        return JsonResult.buildSuccess(productInventoryDTO);
    }

    @RequestMapping("/processBucketOperate")
    public JsonResult processBucketOperate() {
        return inventoryBucketService.processBucketOperate();
    }

    @RequestMapping("/bucketOperateFinishedClear")
    public JsonResult bucketOperateFinishedClear() {
        return inventoryBucketService.bucketOperateFinishedClear();
    }

}
